2
2
.
.
2
2
.
.
1
1
L
L
i
i
t
t
e
e
r
r
a
a
l
l
s
s
I
I
n
n
f
f
o
o
Literal is string representation of both data type and data value: 10, "John", ["AUDI", "FIAT"]
Literal notations are different ways of constructing literals that represent the same data type: 10, +10, 012 or 0xA.
Escape Sequences are used for Character & String Literals to symbolize certain characters that can't be written.
Literal Types
LITERAL
EXAMPLES
DESCRIPTION
Null
null
Null literal represents null data type and value which can only be null.
Boolean
true, false
Boolean literal represents Boolean data type and value which can be true or false.
Character
'C'
Character literal represents character data type and value which can be any character.
Integer
50
Integer literal represents integer data type and value which can be integer.
Long
50
Long Integer literal represents integer data type and value which can be integer.
Float
34.75
Floating literal represents float data type and value which can be decimal.
Double
34.75
Double literal represents double data type and value which can be decimal.
String
"Hello"
String literal represents string data type and value which can be any string.
Array
{0,1,2,3}
Array literal represents array of elements given as a string.
N
N
u
u
l
l
l
l
Null literal represents null data type and can have single value of null.
Null
String name = null;
B
B
o
o
o
o
l
l
e
e
a
a
n
n
Boolean literal represents boolean data type and can have values true or false.
Boolean
boolean access1 = true;
boolean access2 = false;
C
C
h
h
a
a
r
r
a
a
c
c
t
t
e
e
r
r
Character literal
represents character data type and value where value is 16-bit Unicode character
is constructed by enclosing character or escape sequence inside single quotes
Escape sequence
can represent any character, including special ones, as shown by the table below
starts with backslash / escape character followed by specific combinations representing a character
Using octal or Unicode character representations is equivalent to typing that character inside your source code.
This differs from escape sequences \n, \' or \" which will not throw error while '\u000D', '\u0027' or '\u0022' might.
Character
//BASIC CHARACTER LITERAL.
char mark = 'A'; //'A' character.
mark = '\t'; //Tab character.
mark = '\"'; //Double quote character.
mark = '\''; //Single quote character.
//USING OCTAL ESCAPE SEQUENCE.
char mark = '\101'; //'A' character.
mark = '\10'; //Tab quote character.
mark = '\42'; //Double quote character.
mark = '\47'; //Single quote character.
//USING UNICODE ESCAPE SEQUENCE. USES HEXADECIMAL ASCII VALUES WITH LEADING ZEROS.
char mark = '\u0041'; //'A' character.
mark = '\u0009'; //Tab character
mark = '\u0022'; //Double quote character
mark = '\u0027'; //Single quote character reports error.
I
I
n
n
t
t
e
e
g
g
e
e
r
r
Integer literal represents integer data type and value and supports multiple notations.
Integer
int age1 = -41; //Decimal notation (base 10).
int age2 = -051; //Octal notation (base 8 ).
int age3 = -0x29; //Hexadecimal notation (base 16).
L
L
o
o
n
n
g
g
Long literal represents long integer data type and value and supports multiple notations.
Long
long age1 = -41l; //Decimal notation (base 10).
long age2 = -051L; //Octal notation (base 8 ).
long age3 = -0x29L; //Hexadecimal notation (base 16).
F
F
l
l
o
o
a
a
t
t
[
[
R
R
]
]
Float literal represents float data type and value and supports multiple notations.
Float literal is used to represent real number.
Float
double temp1 = 123.045f; //Normal notation.
double temp2 = 1.23045E+2F; //Scientific notation.
D
D
o
o
u
u
b
b
l
l
e
e
[
[
R
R
]
]
Double literal represents double data type and value and supports multiple notations.
Double
double temp1 = 123.045; //Normal notation.
double temp2 = 1.23045E+2; //Scientific notation.
S
S
t
t
r
r
i
i
n
n
g
g
String literal represents string data type and value.
String literal is used to represent strings.
String literal is written by enclosing sequence of characters inside double quotes.
Using octal or Unicode character representations is equivalent to typing that character inside your source code.
This differs from escape sequences \n, \' or \" which will not throw error while '\u000D', '\u0027' or '\u0022' might.
String
//BASIC STRING LITERAL.
String text = "Display letter A"; //"Display letter A" is string literal.
text = "First line \nSecond \t line."; //Escape sequences \n and \t for new line and tab.
//USING OCTAL ESCAPE SEQUENCE.
text = "Display letter \101"; //"Display letter A".
text = "First line \15Second \11 line."; //"First line \nSecond \t line.".
//USING OCTAL ESCAPE SEQUENCE.
text = "Display letter \u0041"; //"Display letter A".
text = "First line \nSecond \u0009 line."; //Replacing \n with unicode causes error.
A
A
r
r
r
r
a
a
y
y
Array literal represents array data type and value.
Array literal is written by enclosing sequence of elements inside curly brackets.
int[] integers = {0,1,2,3}; //{0,1,2,3} is array literal
String[] strings = {"John, Lucy", "Bill"}; //{"John, Lucy", "Bill"} is array literal
E
E
s
s
c
c
a
a
p
p
e
e
S
S
e
e
q
q
u
u
e
e
n
n
c
c
e
e
s
s
Escape Sequences are used for Character & String Literals to symbolize certain characters that can't be written.
Escape Sequences
SEQUENCE
DESCRIPTION
\n
Linefeed
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\e
Escape
\f
Form feed
\\
Backslash
\$
Dollar sign
\"
Double quote (only for Double Quotes and Heredoc)
\'
Single quote (only for Single Quotes and Nowdoc)
\d
Octal representation of character as defined in ASCII Table (For 'A' it is '\101' ).
\ud
Unicode representation of character as defined in ASCII Table (For 'A' it is '\u0041').